package
{
	import nl.hyves.api.valueobjects.UserVO;
	import nl.hyves.api.net.IHyvesResponse;
	import nl.hyves.api.login.WebLogin;
	import nl.hyves.api.login.ILogin;
	import nl.hyves.api.core.IHyvesAPI;
	import nl.hyves.api.core.HyvesAPI;
	import nl.hyves.api.methods.Users;
	import flash.display.Sprite;
	
	public class WebLoginExample extends Sprite
	{
		private static const CONSUMER_KEY:String = 'NTY2MF99lTWIO2JXZC70FFqkUnHI';
		private static const CONSUMER_SECRET:String = 'NTY2MF8Tl4ZJC7AUWd5br7yCEVVs';
		
		private var service:IHyvesAPI;
		
		public function WebLoginExample()
		{
			var methods:Vector.<String> = Vector.<String>([Users.GET_LOGGEDIN]);
			
			service = new HyvesAPI(CONSUMER_KEY, CONSUMER_SECRET);
			
			if (service.isLoggedIn(methods))
			{
				onLoggedIn();
			}
			else
			{
				var login:ILogin = new WebLogin(methods, 'http://localhost/authorized.php');
				service.login(login, onLoggedIn);
			}
			
			service.call(Users.getLoggedin(), onUser);
		}

		private function onLoggedIn():void
		{
			trace('Authenticated! Logged in user has id: ' + service.accesstoken.userid);
		}
		
		private function onUser(response:IHyvesResponse):void
		{
			if (response.success)
			{
				var user:UserVO = response.data as UserVO;
				
				trace(user.firstname + ' ' + user.lastname + ' is logged in.');
			}
			else
			{
				trace(response.error);
			}
		}
	}
}
